##Replication codes for main figures of 
#"Unmasking the rule of law: crime, punishment, and partisanship in comparative perspective"

#Note: use R version 4.1.0

#######################0. preparation for replication
#1)load packages
library(haven)
library(labelled)
library(ggplot2)

#2)load data
ugov_GE <- read_dta("germany_data_ejpr.dta")
ugov_HU <- read_dta("hungary_data_ejpr.dta")
ugov_PL <- read_dta("poland_data_ejpr.dta")
ugov_us_merged <- read_dta("us_data_ejpr.dta")

#3)generate functions to be used
#handy function for 95% CI (http://www.cookbook-r.com/Manipulating_data/Summarizing_data/)

summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  library(plyr)
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}

#and t-test with summary statistics
t.test2 <- function(m1,m2,s1,s2,n1,n2,m0=0,equal.variance=FALSE)
{
  if( equal.variance==FALSE ) 
  {
    se <- sqrt( (s1^2/n1) + (s2^2/n2) )
    # welch-satterthwaite df
    df <- ( (s1^2/n1 + s2^2/n2)^2 )/( (s1^2/n1)^2/(n1-1) + (s2^2/n2)^2/(n2-1) )
  } else
  {
    # pooled standard deviation, scaled by the sample sizes
    se <- sqrt( (1/n1 + 1/n2) * ((n1-1)*s1^2 + (n2-1)*s2^2)/(n1+n2-2) ) 
    df <- n1+n2-2
  }      
  t <- (m1-m2-m0)/se 
  dat <- c(m1-m2, se, t, 2*pt(-abs(t),df))    
  names(dat) <- c("Difference of means", "Std Error", "t", "p-value")
  return(dat) 
}

#######################Replicating figures in main text#########################

######First, Create data objects for each country

#1) Germany
#subsetting relevant population
b<-ugov_GE[which(ugov_GE$treat_1_w2==5),]
c<-ugov_GE[which(ugov_GE$treat_1_w2==6),]
ge_dat_plot<-as.data.frame(rbind(b,c))
rm(b)
rm(c)

#omitting nas
ge_dat_plot<-ge_dat_plot[!is.na(ge_dat_plot$treat_1_2_w2),]
ge_dat_plot<-ge_dat_plot[!is.na(ge_dat_plot$treat_1_w2),]
ge_dat_plot<-ge_dat_plot[!is.na(ge_dat_plot$fine_support_w2),]
ge_dat_plot<-ge_dat_plot[!is.na(ge_dat_plot$fine_amount_w2),]

#creating covid_convern varaible
ge_dat_plot$covid_concern<-NA
ge_dat_plot$covid_concern[which(ge_dat_plot$cvfeelings_w2==1)]<-1
ge_dat_plot$covid_concern[which(ge_dat_plot$cvfeelings_w2==2)]<-1
ge_dat_plot$covid_concern[which(ge_dat_plot$cvfeelings_w2==3)]<-0
ge_dat_plot$covid_concern[which(ge_dat_plot$cvfeelings_w2==4)]<-0
ge_dat_plot<-ge_dat_plot[!is.na(ge_dat_plot$covid_concern),]

#rescaling the dependent variable
ge_dat_plot$fine_support_w2<-5-ge_dat_plot$fine_support_w2
ge_dat_plot$treat_1_w2<-as.factor(ge_dat_plot$treat_1_w2)
ge_dat_plot$treat_1_2_w2<-as.factor(ge_dat_plot$treat_1_2_w2)
ge_dat_plot$covid_concern<-as.factor(ge_dat_plot$covid_concern)
a=which(ge_dat_plot$treat_1_2_w2==1)
b=which(ge_dat_plot$treat_1_2_w2==2)
ge_dat_plot$treat_1_2_w2[a]<-2
ge_dat_plot$treat_1_2_w2[b]<-1

#omitting NAs
ge_dat_plot<-ge_dat_plot[!is.na(ge_dat_plot$fine_amount_w2),]

#generate fine dollars, the variable that normalize the currency as the value of dollar
ge_dat_plot$fine_amount_w2_dollars<-ge_dat_plot$fine_amount_w2*1.12

#2) Hungary
#making binary concern variable
ugov_HU$covid_concern<-NA
ugov_HU$covid_concern[which(ugov_HU$yougov_cv_feel==1)]<-1
ugov_HU$covid_concern[which(ugov_HU$yougov_cv_feel==2)]<-1
ugov_HU$covid_concern[which(ugov_HU$yougov_cv_feel==3)]<-0
ugov_HU$covid_concern[which(ugov_HU$yougov_cv_feel==4)]<-0

#subset data that are only needed with creating types and fine binary variables

#subsetting fined most-liked MP
a<-ugov_HU[which(ugov_HU$most_liked_party_HU==1&ugov_HU$mask_1_treatment==2&ugov_HU$mask_2_treatment==1),]
b<-ugov_HU[which(ugov_HU$most_liked_party_HU==2&ugov_HU$mask_1_treatment==3&ugov_HU$mask_2_treatment==1),]
f_ml<-rbind(a,b)
f_ml$fine_bin<-1
f_ml$type<-2

#subsetting not fined most-liked MP
a<-ugov_HU[which(ugov_HU$most_liked_party_HU==1&ugov_HU$mask_1_treatment==2&ugov_HU$mask_2_treatment==2),]
b<-ugov_HU[which(ugov_HU$most_liked_party_HU==2&ugov_HU$mask_1_treatment==3&ugov_HU$mask_2_treatment==2),]
nf_ml<-rbind(a,b)
nf_ml$fine_bin<-0
nf_ml$type<-2

#subsetting fined least-liked MP
a<-ugov_HU[which(ugov_HU$most_liked_party_HU==1&ugov_HU$mask_1_treatment==3&ugov_HU$mask_2_treatment==1),]
b<-ugov_HU[which(ugov_HU$most_liked_party_HU==2&ugov_HU$mask_1_treatment==2&ugov_HU$mask_2_treatment==1),]
c<-ugov_HU[which(ugov_HU$most_liked_party_HU==9&ugov_HU$mask_1_treatment==2&ugov_HU$mask_2_treatment==1),]
d<-ugov_HU[which(ugov_HU$most_liked_party_HU==9&ugov_HU$mask_1_treatment==3&ugov_HU$mask_2_treatment==1),]
f_ll<-rbind(a,b,c,d)
f_ll$fine_bin<-1
f_ll$type<-3

#subsetting not fined most-liked MP
a<-ugov_HU[which(ugov_HU$most_liked_party_HU==1&ugov_HU$mask_1_treatment==3&ugov_HU$mask_2_treatment==2),]
b<-ugov_HU[which(ugov_HU$most_liked_party_HU==2&ugov_HU$mask_1_treatment==2&ugov_HU$mask_2_treatment==2),]
c<-ugov_HU[which(ugov_HU$most_liked_party_HU==9&ugov_HU$mask_1_treatment==2&ugov_HU$mask_2_treatment==2),]
d<-ugov_HU[which(ugov_HU$most_liked_party_HU==9&ugov_HU$mask_1_treatment==3&ugov_HU$mask_2_treatment==2),]
nf_ll<-rbind(a,b,c,d)
nf_ll$fine_bin<-0
nf_ll$type<-3

#aggregate all the created objects 
hu_dat_plot<-rbind(f_ll,f_ml,nf_ll,nf_ml)
hu_dat_plot$fine_bin<-as.factor(hu_dat_plot$fine_bin) #make the fine binary variable as a factor
hu_dat_plot$type<-as.factor(hu_dat_plot$type) #make the type variable as a factor
hu_dat_plot$covid_concern<-as.factor(hu_dat_plot$covid_concern) #make covid concern variable as a factor
hu_dat_plot<-hu_dat_plot[!is.na(hu_dat_plot$mask_fine),] #omit NAs in dependent varaible

#make fine amount appropriate for the value of Hungarian currency
hu_dat_plot$mask_fine<-hu_dat_plot$mask_fine*1000

#generate fine dollars, the variable that normalize the currency as the value of dollar
hu_dat_plot$mask_fine_dollars<-hu_dat_plot$mask_fine*0.0034

#3) US

#create partisanship variable
ugov_us_merged$most_liked_party<-NA
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==1)]<-1 
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==2)]<-1 
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==3)]<-1 #dem
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==5)]<-2
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==6)]<-2 #rep
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==7)]<-2 #rep
ugov_us_merged$most_liked_party[which(ugov_us_merged$pid7==4)]<-3 #ind

#making binary concern variable
ugov_us_merged$covid_concern<-NA
ugov_us_merged$covid_concern[which(ugov_us_merged$yougov_cv_feel==1)]<-1
ugov_us_merged$covid_concern[which(ugov_us_merged$yougov_cv_feel==2)]<-1
ugov_us_merged$covid_concern[which(ugov_us_merged$yougov_cv_feel==3)]<-0
ugov_us_merged$covid_concern[which(ugov_us_merged$yougov_cv_feel==4)]<-0

#subset data that are only needed with creating types and fine binary variables

#subsetting fined most-liked MP
a<-ugov_us_merged[which(ugov_us_merged$most_liked_party==1&ugov_us_merged$mask_1_treatment==2&ugov_us_merged$mask_2_treatment==1),]
b<-ugov_us_merged[which(ugov_us_merged$most_liked_party==2&ugov_us_merged$mask_1_treatment==3&ugov_us_merged$mask_2_treatment==1),]
f_ml<-rbind(a,b)
f_ml$fine_bin<-1
f_ml$type<-2

#subsetting not fined most-liked MP
a<-ugov_us_merged[which(ugov_us_merged$most_liked_party==1&ugov_us_merged$mask_1_treatment==2&ugov_us_merged$mask_2_treatment==2),]
b<-ugov_us_merged[which(ugov_us_merged$most_liked_party==2&ugov_us_merged$mask_1_treatment==3&ugov_us_merged$mask_2_treatment==2),]
nf_ml<-rbind(a,b)
nf_ml$fine_bin<-0
nf_ml$type<-2

#subsetting fined least-liked MP
a<-ugov_us_merged[which(ugov_us_merged$most_liked_party==1&ugov_us_merged$mask_1_treatment==3&ugov_us_merged$mask_2_treatment==1),]
b<-ugov_us_merged[which(ugov_us_merged$most_liked_party==2&ugov_us_merged$mask_1_treatment==2&ugov_us_merged$mask_2_treatment==1),]
c<-ugov_us_merged[which(ugov_us_merged$most_liked_party==3&ugov_us_merged$mask_1_treatment==2&ugov_us_merged$mask_2_treatment==1),]
d<-ugov_us_merged[which(ugov_us_merged$most_liked_party==3&ugov_us_merged$mask_1_treatment==3&ugov_us_merged$mask_2_treatment==1),]
f_ll<-rbind(a,b,c,d)
f_ll$fine_bin<-1
f_ll$type<-3

#subsetting not fined least-liked MP
a<-ugov_us_merged[which(ugov_us_merged$most_liked_party==1&ugov_us_merged$mask_1_treatment==3&ugov_us_merged$mask_2_treatment==2),]
b<-ugov_us_merged[which(ugov_us_merged$most_liked_party==2&ugov_us_merged$mask_1_treatment==2&ugov_us_merged$mask_2_treatment==2),]
c<-ugov_us_merged[which(ugov_us_merged$most_liked_party==3&ugov_us_merged$mask_1_treatment==2&ugov_us_merged$mask_2_treatment==2),]
d<-ugov_us_merged[which(ugov_us_merged$most_liked_party==3&ugov_us_merged$mask_1_treatment==3&ugov_us_merged$mask_2_treatment==2),]
nf_ll<-rbind(a,b,c,d)
nf_ll$fine_bin<-0
nf_ll$type<-3

#aggregate all the created objects 
us_dat_plot<-rbind(f_ll,f_ml,nf_ll,nf_ml)
us_dat_plot$fine_bin<-as.factor(us_dat_plot$fine_bin) #make the fine binary variable as a factor
us_dat_plot$type<-as.factor(us_dat_plot$type) #make the type variable as a factor
us_dat_plot$covid_concern<-as.factor(us_dat_plot$covid_concern) #make covid concern variable as a factor
us_dat_plot<-us_dat_plot[!is.na(us_dat_plot$mask_fine),] #omit NAs in dependent varaible

#4) Poland

#making binary concern variable
ugov_PL$covid_concern<-NA
ugov_PL$covid_concern[which(ugov_PL$yougov_cv_feel==1)]<-1
ugov_PL$covid_concern[which(ugov_PL$yougov_cv_feel==2)]<-1
ugov_PL$covid_concern[which(ugov_PL$yougov_cv_feel==3)]<-0
ugov_PL$covid_concern[which(ugov_PL$yougov_cv_feel==4)]<-0
ugov_PL$covid_concern[which(ugov_PL$yougov_cv_feel==5)]<-0

#subset data that are only needed with creating types and fine binary variables

#subsetting fined most-liked MP
a<-ugov_PL[which(ugov_PL$party_pick==1&ugov_PL$mask_1_treatment==2&ugov_PL$mask_2_treatment==1),]
b<-ugov_PL[which(ugov_PL$party_pick==2&ugov_PL$mask_1_treatment==3&ugov_PL$mask_2_treatment==1),]
f_ml<-rbind(a,b)
f_ml$fine_bin<-1
f_ml$type<-2

#subsetting not fined most-liked MP
a<-ugov_PL[which(ugov_PL$party_pick==1&ugov_PL$mask_1_treatment==2&ugov_PL$mask_2_treatment==2),]
b<-ugov_PL[which(ugov_PL$party_pick==2&ugov_PL$mask_1_treatment==3&ugov_PL$mask_2_treatment==2),]
nf_ml<-rbind(a,b)
nf_ml$fine_bin<-0
nf_ml$type<-2

#subsetting fined least-liked MP
a<-ugov_PL[which(ugov_PL$party_pick==1&ugov_PL$mask_1_treatment==3&ugov_PL$mask_2_treatment==1),]
b<-ugov_PL[which(ugov_PL$party_pick==2&ugov_PL$mask_1_treatment==2&ugov_PL$mask_2_treatment==1),]
f_ll<-rbind(a,b)
f_ll$fine_bin<-1
f_ll$type<-3

#subsetting not fined least-liked MP
a<-ugov_PL[which(ugov_PL$party_pick==1&ugov_PL$mask_1_treatment==3&ugov_PL$mask_2_treatment==2),]
b<-ugov_PL[which(ugov_PL$party_pick==2&ugov_PL$mask_1_treatment==2&ugov_PL$mask_2_treatment==2),]
nf_ll<-rbind(a,b)
nf_ll$fine_bin<-0
nf_ll$type<-3

#aggregate all the created objects 
pl_dat_plot<-rbind(f_ll,f_ml,nf_ll,nf_ml)
pl_dat_plot$fine_bin<-as.factor(pl_dat_plot$fine_bin) #make the fine binary variable as a factor
pl_dat_plot$type<-as.factor(pl_dat_plot$type) #make the treatement type variable as a factor 
pl_dat_plot$covid_concern<-as.factor(pl_dat_plot$covid_concern) #make the covid concern variable as a factor
pl_dat_plot<-pl_dat_plot[!is.na(pl_dat_plot$mask_fine),] #omit NAs in dependent variable

#make fine amount appropriate for the value of Polish currency
pl_dat_plot$mask_fine<-pl_dat_plot$mask_fine*10

#generate fine dollars, the variable that normalize the currency as the value of dollar
pl_dat_plot$mask_fine_dollars<-pl_dat_plot$mask_fine*0.26

#remove all unnecessary objects
rm(a,b,c,d,f_ll,f_ml,nf_ll,nf_ml)

######Now, we are ready to create main plots

#Figure 1
figure1<-vector(mode="list",length=4)

ge_app_1 <- summarySE(ge_dat_plot, measurevar="fine_amount_w2_dollars", groupvars=c("treat_1_w2"))

figure1[[1]]<-ggplot(ge_app_1, aes(x=treat_1_w2, y=fine_amount_w2_dollars)) + 
  geom_errorbar(aes(ymin=fine_amount_w2_dollars-ci, ymax=fine_amount_w2_dollars+ci), width=.1) +
  geom_line() +
  geom_point()+
  ylim(300,500)+
  scale_x_discrete(name =" ", 
                   labels=c("5" = "Most-Liked",
                            "6" = "Least-Liked"))+
  labs(y="Fine Amount (Dollar)",title = "Germany")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),axis.text.x=element_text(size=11))


us_app_1<-summarySE(us_dat_plot, measurevar="mask_fine", groupvars=c("type"))

figure1[[2]]<-ggplot(us_app_1, aes(x=type, y=mask_fine)) + 
  geom_errorbar(aes(ymin=mask_fine-ci, ymax=mask_fine+ci), width=.1) +
  geom_line() +
  geom_point()+
  ylim(300,500)+
  scale_x_discrete(name =" ", 
                   labels=c("2" = "Most-Liked",
                            "3" = "Least-Liked"))+
  labs(y="Fine Amount (Dollar)",title = "The United States")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),axis.text.x=element_text(size=11))

hu_app_1 <- summarySE(hu_dat_plot, measurevar="mask_fine_dollars", groupvars=c("type"))

figure1[[3]]<-ggplot(hu_app_1, aes(x=type, y=mask_fine_dollars)) + 
  geom_errorbar(aes(ymin=mask_fine_dollars-ci, ymax=mask_fine_dollars+ci), width=.1) +
  geom_line() +
  geom_point()+
  ylim(300,500)+
  scale_x_discrete(name =" ", 
                   labels=c("2" = "Most-Liked",
                            "3" = "Least-Liked"))+
  labs(y="Fine Amount (Dollar)",title = "Hungary")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),axis.text.x=element_text(size=11))

pl_app_1<-summarySE(pl_dat_plot, measurevar="mask_fine_dollars", groupvars=c("type"))

figure1[[4]]<-ggplot(pl_app_1, aes(x=type, y=mask_fine_dollars)) + 
  geom_errorbar(aes(ymin=mask_fine_dollars-ci, ymax=mask_fine_dollars+ci), width=.1) +
  geom_line() +
  geom_point()+
  ylim(300,500)+
  scale_x_discrete(name =" ", 
                   labels=c("2" = "Most-Liked",
                            "3" = "Least-Liked"))+
  labs(y="Fine Amount (Dollar)",title = "Poland")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),axis.text.x=element_text(size=11))

ggsave("figure1.pdf", width=10, height=5,gridExtra::marrangeGrob(grobs = figure1, nrow=1, ncol=4,top=NULL))

#figure2
figure2<-vector(mode="list",length=4)

ge_app_2 <- summarySE(ge_dat_plot, measurevar="fine_amount_w2_dollars", groupvars=c("treat_1_w2","covid_concern"))
ge_app_2 <-na.omit(ge_app_2)
pd <- position_dodge(0.3)

figure2[[1]]<-ggplot(ge_app_2, aes(x=treat_1_w2, y=fine_amount_w2_dollars, colour=covid_concern)) + 
  geom_errorbar(aes(ymin=fine_amount_w2_dollars-ci, ymax=fine_amount_w2_dollars+ci), width=.1,position=pd) +
  geom_line(position = pd) +
  geom_point(position = pd)+
  ylim(250,500)+
  scale_x_discrete(name =" ", 
                   labels=c("5" = "Most-Liked",
                            "6" = "Least-Liked"))+
  scale_colour_manual(name="Covid Concern",    # Legend label, use darker colors
                      breaks=c("0", "1"),
                      labels=c("Not Worried", "Worried"),
                      values = c("grey50","black"))+
  labs(y="Fine Amount (Dollar)",title = "Germany")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),legend.position = "none",axis.text.x=element_text(size=11))

us_app_2 <- summarySE(us_dat_plot, measurevar="mask_fine", groupvars=c("type","covid_concern"))
us_app_2 <-na.omit(us_app_2)
pd <- position_dodge(0.3)

figure2[[2]]<-ggplot(us_app_2, aes(x=type, y=mask_fine, colour=covid_concern)) + 
  geom_errorbar(aes(ymin=mask_fine-ci, ymax=mask_fine+ci), width=.1,position=pd) +
  geom_line(position = pd) +
  geom_point(position = pd)+
  ylim(250,500)+
  scale_x_discrete(name =" ", 
                   labels=c("2" = "Most-Liked",
                            "3" = "Least-Liked"))+
  scale_colour_manual(name="Covid Concern",    # Legend label, use darker colors
                      breaks=c("0", "1"),
                      labels=c("Not Worried", "Worried"),
                      values = c("grey50","black"))+
  labs(y="Fine Amount (Dollar)",title = "The United States")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),legend.position = "none",axis.text.x=element_text(size=11))

hu_app_2 <- summarySE(hu_dat_plot, measurevar="mask_fine_dollars", groupvars=c("type","covid_concern"))
hu_app_2 <-na.omit(hu_app_2)
pd <- position_dodge(0.3)

figure2[[3]]<-ggplot(hu_app_2, aes(x=type, y=mask_fine_dollars, colour=covid_concern)) + 
  geom_errorbar(aes(ymin=mask_fine_dollars-ci, ymax=mask_fine_dollars+ci), width=.1,position=pd) +
  geom_line(position = pd) +
  geom_point(position = pd)+
  ylim(250,500)+
  scale_x_discrete(name =" ", 
                   labels=c("2" = "Most-Liked",
                            "3" = "Least-Liked"))+
  scale_colour_manual(name="Covid Concern",    # Legend label, use darker colors
                      breaks=c("0", "1"),
                      labels=c("Not Worried", "Worried"),
                      values = c("grey50","black"))+
  labs(y="Fine Amount (Dollar)",title = "Hungary")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),legend.position = "none",axis.text.x=element_text(size=11))

pl_app_2 <- summarySE(pl_dat_plot, measurevar="mask_fine_dollars", groupvars=c("type","covid_concern"))
pl_app_2 <-na.omit(pl_app_2)
pd <- position_dodge(0.3)

figure2[[4]]<-ggplot(pl_app_2, aes(x=type, y=mask_fine_dollars, colour=covid_concern)) + 
  geom_errorbar(aes(ymin=mask_fine_dollars-ci, ymax=mask_fine_dollars+ci), width=.1,position=pd) +
  geom_line(position = pd) +
  geom_point(position = pd)+
  ylim(250,500)+
  scale_x_discrete(name =" ", 
                   labels=c("2" = "Most-Liked",
                            "3" = "Least-Liked"))+
  scale_colour_manual(name="Covid Concern",    # Legend label, use darker colors
                      breaks=c("0", "1"),
                      labels=c("Not Worried", "Worried"),
                      values = c("grey50","black"))+
  labs(y="Fine Amount (Dollar)",title = "Poland")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),legend.position = c(0.65, 0.18),axis.text.x=element_text(size=11))

ggsave("figure2.pdf", width=10, height=5,gridExtra::marrangeGrob(grobs = figure2, nrow=1, ncol=4,top=NULL))

###Figure A1.

ge_dat_plot_d<-ge_dat_plot[!(ge_dat_plot$treat_1_w2==5&is.na(ge_dat_plot$most_liked_party_w2)),]
ge_dat_plot_d<-ge_dat_plot_d[!(ge_dat_plot_d$treat_1_w2==6&is.na(ge_dat_plot_d$least_liked_party_w2)),]

which(ge_dat_plot_d$treat_1_w2==5&is.na(ge_dat_plot_d$most_liked_party_w2)) #should be NA now
which(ge_dat_plot_d$treat_1_w2==6&is.na(ge_dat_plot_d$least_liked_party_w2)) #should be NA now


figureA1<-vector(mode="list",length=4)

ge_app_1 <- summarySE(ge_dat_plot, measurevar="fine_amount_w2_dollars", groupvars=c("treat_1_w2"))

figureA1[[1]]<-ggplot(ge_app_1, aes(x=treat_1_w2, y=fine_amount_w2_dollars)) + 
  geom_errorbar(aes(ymin=fine_amount_w2_dollars-ci, ymax=fine_amount_w2_dollars+ci), width=.1) +
  geom_line() +
  geom_point()+
  ylim(300,500)+
  scale_x_discrete(name =" ", 
                   labels=c("5" = "Most-Liked",
                            "6" = "Least-Liked"))+
  labs(y="Fine Amount (Dollar)",title = "Germany")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),axis.text.x=element_text(size=11))

ge_app_2 <- summarySE(ge_dat_plot_d, measurevar="fine_amount_w2_dollars", groupvars=c("treat_1_w2"))

figureA1[[3]]<-ggplot(ge_app_2, aes(x=treat_1_w2, y=fine_amount_w2_dollars)) + 
  geom_errorbar(aes(ymin=fine_amount_w2_dollars-ci, ymax=fine_amount_w2_dollars+ci), width=.1) +
  geom_line() +
  geom_point()+
  ylim(300,500)+
  scale_x_discrete(name =" ", 
                   labels=c("5" = "Most-Liked",
                            "6" = "Least-Liked"))+
  labs(y="Fine Amount (Dollar)",title = "Germany")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),axis.text.x=element_text(size=11))

ge_app_3 <- summarySE(ge_dat_plot, measurevar="fine_amount_w2_dollars", groupvars=c("treat_1_w2","covid_concern"))
ge_app_3 <-na.omit(ge_app_3)
pd <- position_dodge(0.3)

figureA1[[2]]<-ggplot(ge_app_3, aes(x=treat_1_w2, y=fine_amount_w2_dollars, colour=covid_concern)) + 
  geom_errorbar(aes(ymin=fine_amount_w2_dollars-ci, ymax=fine_amount_w2_dollars+ci), width=.1,position=pd) +
  geom_line(position = pd) +
  geom_point(position = pd)+
  ylim(250,500)+
  scale_x_discrete(name =" ", 
                   labels=c("5" = "Most-Liked",
                            "6" = "Least-Liked"))+
  scale_colour_manual(name="Covid Concern",    # Legend label, use darker colors
                      breaks=c("0", "1"),
                      labels=c("Not Worried", "Worried"),
                      values = c("grey50","black"))+
  labs(y="Fine Amount (Dollar)",title = "Germany")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),legend.position = "none",axis.text.x=element_text(size=11))

ge_app_4 <- summarySE(ge_dat_plot_d, measurevar="fine_amount_w2_dollars", groupvars=c("treat_1_w2","covid_concern"))
ge_app_4 <-na.omit(ge_app_4)
pd <- position_dodge(0.3)

figureA1[[4]]<-ggplot(ge_app_4, aes(x=treat_1_w2, y=fine_amount_w2_dollars, colour=covid_concern)) + 
  geom_errorbar(aes(ymin=fine_amount_w2_dollars-ci, ymax=fine_amount_w2_dollars+ci), width=.1,position=pd) +
  geom_line(position = pd) +
  geom_point(position = pd)+
  ylim(250,500)+
  scale_x_discrete(name =" ", 
                   labels=c("5" = "Most-Liked",
                            "6" = "Least-Liked"))+
  scale_colour_manual(name="Covid Concern",    # Legend label, use darker colors
                      breaks=c("0", "1"),
                      labels=c("Not Worried", "Worried"),
                      values = c("grey50","black"))+
  labs(y="Fine Amount (Dollar)",title = "Germany")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5),legend.position = c(0.65, 0.18),axis.text.x=element_text(size=11))

ggsave("figureA1.pdf", width=5, height=10,gridExtra::marrangeGrob(grobs = figureA1, nrow=2, ncol=2,top=NULL))
